package com.swpu.service_xueya.controller;


import com.swpu.commonutils.JwtUtils;
import com.swpu.commonutils.R;
import com.swpu.commonutils.ResultEncryption;
import com.swpu.service_user.aop.Decrypt;
import com.swpu.service_user.aop.Encrypt;
import com.swpu.service_xueya.entity.XueyaRecord;
import com.swpu.service_xueya.entity.vo.XueyaChartData;
import com.swpu.service_xueya.entity.vo.XueyaRecordListVo;
import com.swpu.service_xueya.entity.vo.XueyaRecordVo2;
import com.swpu.service_xueya.service.XueyaRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chaichai
 * @since 2024-04-17
 */
@RestController
@RequestMapping("/service_xueya/xueya-record")
public class XueyaRecordController {
    @Autowired
    private XueyaRecordService xueyaRecordService;
    @PostMapping("addXueyaData")
    @Decrypt
    @Encrypt
    public R addXueyaData(@RequestBody ResultEncryption resultEncryption, XueyaRecord xueyaRecord, HttpServletRequest request){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户无访问权限");
        }
        String recordId = xueyaRecordService.addXueyaData(openid, xueyaRecord);
        return R.ok().data("recordId",recordId);
    }
    @GetMapping("getXueyaDataNewest")
    public R GetXueyaDataById(HttpServletRequest request){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户无访问权限");
        }
        XueyaRecord xueyaRecord = xueyaRecordService.getXueyaDataNewest(openid);
        return R.ok().data("xueyaRecord",xueyaRecord);
    }

    @GetMapping("getXueyaDataById")
    public R GetXueyaDataById(HttpServletRequest request,@RequestParam String recordId){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户无访问权限");
        }
        XueyaRecord xueyaRecord = xueyaRecordService.getXueyaDataById(recordId);
        return R.ok().data("xueyaRecord",xueyaRecord);
    }
    @GetMapping("getChartData")
    public R getChartData(HttpServletRequest request){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户未登录");
        }
        XueyaChartData chartData = xueyaRecordService.getChartData(openid);
        return R.ok().data("chartData",chartData);
    }

    @GetMapping("getXueyaDataList")
    public R getXueyaDataList(HttpServletRequest request,@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "20") Integer pageSize){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户未登录");
        }
        List<XueyaRecordListVo> xueyaDataList = xueyaRecordService.getXueyaDataList(openid, pageNum, pageSize);
        return R.ok().data("xueyaDataList",xueyaDataList);
    }
    @GetMapping("getRecentMonthXueyaStatus")
    public R getRecentMonthXueyaStatus(HttpServletRequest request){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户未登录");
        }
        Map resultMap = xueyaRecordService.getRecentMonthXueyaStatus(openid);
        return R.ok().data(resultMap);
    }

}

